package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.dto.CouponDto;
import com.design.movieticket.entity.Balance;
import com.design.movieticket.entity.Coupon;
import com.design.movieticket.entity.Coupon;
import com.design.movieticket.entity.UserCoupon;
import com.design.movieticket.mapper.BalanceMapper;
import com.design.movieticket.mapper.CouponMapper;
import com.design.movieticket.mapper.UserCouponMapper;
import com.design.movieticket.service.CouponService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
    @Resource
    private CouponMapper couponMapper;

    @Resource
    private BalanceMapper balanceMapper;

    @Resource
    private UserCouponMapper userCouponMapper;

    /** 分页查询 */
    @Override
    public LambdaQueryWrapper buildQuery(Coupon coupon) {
        LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(Coupon.class)
                .like(StringUtils.isNotBlank(coupon.getCouponName()),Coupon::getCouponName,coupon.getCouponName())
                .like(StringUtils.isNotBlank(coupon.getCouponType()),Coupon::getCouponType,coupon.getCouponType());
        return lambdaQueryWrapper;
    }

    /** 分页相关 */
    @Override
    public <E extends IPage<Coupon>> E page(E page, Wrapper<Coupon> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    /** 新增或更新优惠卷数据 */
    @Override
    public void addOrUpdateCouponInfo(Coupon coupon) {
        super.saveOrUpdate(coupon);
    }

    /** 获取优惠卷信息详情 */
    @Override
    public Coupon getCouponDetail(long id) {
        return super.getById(id);
    }

    /** 删除优惠卷信息 */
    @Override
    public void delCouponById(long id) {
        super.removeById(id);
    }




    @Override
    public List<Coupon> wxGetCouponList() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = simpleDateFormat.format(date);
        return couponMapper.wxGetCouponList(nowTime);
    }

    @Override
    public List<CouponDto> wxGetMyCouponList(Long userId) {
        return couponMapper.wxGetMyCouponList(userId);
    }

    @Override
    public int wxConversionCoupon(Coupon coupon, Long userId) {
        Balance userBalance = balanceMapper.selectOne(new LambdaQueryWrapper<Balance>().eq(Balance::getUserId,userId));
        BigDecimal userPoints = userBalance.getPoints();
        if (userPoints.compareTo(coupon.getNeedPoints())==-1){
            return 0;
        }
        else {
            userBalance.setPoints(userPoints.subtract(coupon.getNeedPoints()));
            UserCoupon userCoupon = new UserCoupon();
            userCoupon.setUserId(userId);
            userCoupon.setCoupon_id(coupon.getId());
            userCouponMapper.insert(userCoupon);
            balanceMapper.update(userBalance,new LambdaQueryWrapper<Balance>().eq(Balance::getUserId,userId));
            return 1;
        }
    }
}
